#!/usr/bin/env python3
import rclpy
from rclpy.node import Node
from std_msgs.msg import String

class KeyboardNode(Node):
    def __init__(self):
        super().__init__('keyboard_node')
        self.pub = self.create_publisher(String, '/keyboard_command', 10)
        self.get_logger().info('键盘监听器启动，输入 v(oice)) 开始语言交互，输入t(ext)文字交互')
        self.get_logger().info('不要有标点如：配送物体到绿色区域再到蓝色区域最后到红色区域')
        self.timer = self.create_timer(0.1, self.check_input)
        self.ready = True

    def check_input(self):
        if self.ready:
            self.ready = False
            user_input = input('>>> ').strip().lower()
            if user_input == 'v':
                self.get_logger().info('请在 1.5 秒内输入语言')
            elif user_input == 't':
                self.get_logger().info('请在 10 秒内输入文字')

            self.pub.publish(String(data=user_input))
            self.ready = True

def main():
    rclpy.init()
    node = KeyboardNode()
    try:
        rclpy.spin(node)
    except KeyboardInterrupt:
        pass
    rclpy.shutdown()

if __name__ == '__main__':
    main()
